<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Lxk_auth Class
 *
 * @author Li Xiao Kai
 * @since Version 1.0
 * @thinks  http://github.com/benedmunds/CodeIgniter-Ion-Auth
 */

// ------------------------------------------------------------------------

class Auth
{
	protected $CI;

	public $error_msg = array();

	public function __construct()
	{
		$this->CI = &get_instance();

		$this->CI->load->model('user_model');

		log_message('debug', "Auth Class Initialized");
	}

	/**
	 * 登录操作
	 *
	 * @param $username string 用户帐号
	 * @param $password string 用户密码
	 * @return bool
	 */
	public function login($username, $password, $remember = FALSE)
	{
		if (empty($username) || empty($password))
		{
			$this->set_error('用户名或密码不能为空');
			return FALSE;
		}

		$user = $this->CI->user_model->get_by('username', $username);

		if (count($user) < 1)
		{
			$this->set_error('用户不存在');
			return FALSE;
		}
		if ($user['password'] !== md5($password))
		{
			$this->set_error('账号或是密码错误，请重新登录');
			return FALSE;
		}
		//只有激活用户才可以登录
		if ($user['state'] == 0)
		{
			//等待优化，比如记录session，然后跳转到激活提示页面等
			$this->set_error('该账号还未激活');
			return FALSE;
		}

		//更新登录时间 + 添加登录日志
		$this->CI->user_model->update_last_login($user);

		//登录后保存到SESSION的数据
		$session_data = array(
			'uid' => $user['uid'], //用户id
			'username' => $user['username'], //用户帐号
			'realname' => $user['realname'], //用户姓名
			'email' => $user['email'], //用户邮箱
			'group_id' => $user['group_id'], //用户组id
			'last_login_time' => time(), //最后一次访问时间
			//'last_login_ip' => $this->ci->input->ip_address(), //最后一次登录IP
			);
		$this->CI->session->set_userdata($session_data);

		return TRUE;
	}

	/**
	 * 退出操作
	 *
	 */
	public function logout()
	{
		$unset_userdata = array(
			'uid',
			'username',
			'realname',
			'email',
			'group_id',
			'last_login_time');
		$this->CI->session->unset_userdata($unset_userdata);
		$this->CI->session->sess_destroy();

		return TRUE;
	}

	/**
	 * 注册操作
	 */
	public function register($post)
	{
		if (empty($post))
		{
			$this->set_error('提交内容不能为空');
			return FALSE;
		}
		//表单验证时必须加上必填项验证，否则下面必须开启且需要更多有效的验证规则
		/*
		* if (isset($post['ifbroker']) && empty($post['ifbroker']))
		* {
		* $this->set_error('注册类型必须选择');
		* return false;
		* }
		* if (isset($post['username']) && empty($post['username']))
		* {
		* $this->set_error('用户帐号不能为空');
		* return false;
		* }
		* if (isset($post['password']) && empty($post['password']))
		* {
		* $this->set_error('用户密码不能为空');
		* return false;
		* }
		* if (isset($post['email']) && empty($post['email']))
		* {
		* $this->set_error('用户邮件不能为空');
		* return false;
		* }
		* if (isset($post['mobile']) && empty($post['mobile']))
		* {
		* $this->set_error('手机号码不能为空');
		* return false;
		* }
		*/

		$this->CI->user_model->create_user($post);

		return TRUE;
	}

	public function update_user()
	{

	}

	public function delete_user()
	{

	}

	public function forgotten_password()
	{

	}

	public function forgotten_password_complete()
	{

	}

	/**
	 * 登录判断
	 *
	 * @return bool
	 */
	public function logged_in()
	{
		return (bool)$this->CI->session->userdata('uid');
	}

	/**
	 * 创始人判断
	 *
	 * @return bool
	 */
	public function is_admin($uid = NULL)
	{
		$uid = $uid ? $uid : $this->CI->session->userdata('uid');
		return $uid == 1;
	}

	public function in_group()
	{

	}

	public function username_check($username)
	{
		return count($this->CI->user_model->get_by('username', $username)) > 0;
	}

	public function email_check()
	{

	}

	public function identity_check()
	{

	}

	public function user()
	{

	}

	public function users()
	{

	}

	public function group()
	{

	}

	public function groups()
	{

	}

	public function messages()
	{

	}

	public function set_message_delimiters()
	{

	}

	public function errors()
	{

	}

	public function set_error_delimiters()
	{

	}

	public function trigger_events()
	{

	}

	public function set_hook()
	{

	}


	/**
	 * Set an error message
	 *
	 * @param	$msg string
	 * @return	void
	 */
	public function set_error($msg)
	{
		if (is_array($msg))
		{
			foreach ($msg as $val)
			{
				$this->error_msg[] = $val;
			}
		}
		else
		{
			$this->error_msg[] = $msg;
		}

		log_message('error', $msg);
	}

	/**
	 * Display the error message
	 *
	 * @param	$open string 开始附加字符
	 * @param	$close string 结束附加字符
	 * @return	string
	 */
	public function display_errors($open = '', $close = '')
	{
		$str = '';
		foreach ($this->error_msg as $val)
		{
			$str .= $open . $val . $close;
		}

		return $str;
	}
}

/* End of file auth.php */
/* Location: ./application/library/auth.php */
